<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流水号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select placeholder="请选择用户类型" v-model="queryParams.userType" size="small">
          <el-option label="陪诊" value="20"></el-option>
          <el-option label="护工" value="30"></el-option>
          <el-option label="医生" value="40"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缴费状态" prop="payStatus">
        <el-select placeholder="请选择缴费状态" v-model="queryParams.payStatus" size="small">
          <el-option label="已支付" value="1"></el-option>
          <el-option label="退款中" value="2"></el-option>
          <el-option label="已退回" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <span style="margin-left:5px;font-size: 20px" >保证金总额：{{totalAmount}}元</span>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" prop="" width="50" align="center">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"> </span>
        </template>
      </el-table-column>
      <el-table-column label="流水号" align="center" prop="serialNumber" >
        <template slot-scope="scope">
          <span style="color: #1c84c6">{{scope.row.serialNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="用户类型" align="center" prop="userType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_type" :value="scope.row.userType"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="保证金金额(元)" align="center" prop="amount" />
      <el-table-column label="缴费时间" align="center" prop="payTime"/>
      <el-table-column label="缴费状态" align="center" prop="payStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.payStatus === '1'" style="color: green">已支付</span>
          <span v-if="scope.row.payStatus === '2'" style="color: #1c84c6">退款中</span>
          <span v-if="scope.row.payStatus === '3'" style="color: red">已退回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            v-if="scope.row.payStatus === '2' && scope.row.refundAuditStatus === '0'"
            @click="handleAudit(scope.row)"
            v-hasPermi="['biz:money:audit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核状态" prop="refundAuditStatus">
          <el-radio v-model="form.refundAuditStatus" label="1">审核通过</el-radio>
          <el-radio v-model="form.refundAuditStatus" label="2">审核不通过</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="refundRemark">
          <el-input v-model="form.refundRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMoney, getMoney, delMoney, addMoney, updateMoney, changeStatus,audit,getAllSureAmount } from "@/api/biz/money";

export default {
  name: "Money",
  dicts:['biz_sure_money_status','sys_user_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保证金表格数据
      moneyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        userName: undefined,
        type: undefined,
        serialNumber: undefined,
        phonenumber: undefined,
        amount: undefined,
        payTime: undefined,
        payStatus: undefined,
        refundAuditStatus: undefined,
        refundRemark: undefined,
      },
      // 表单参数
      form: {},
      submitLoading: false,
      totalAmount:undefined,
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        refundAuditStatus: [
          { required: true, message: "退款审核状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAllSureAmount();
  },
  methods: {
    /** 查询保证金列表 */
    getList() {
      this.loading = true;
      listMoney(this.queryParams).then(response => {
        this.moneyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllSureAmount(){
      getAllSureAmount().then(res =>{
        console.log("res",res);
        this.totalAmount = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        type: null,
        serialNumber: null,
        phone: null,
        amount: null,
        payTime: null,
        payStatus: "0",
        refundAuditStatus: "0",
        refundRemark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保证金";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMoney(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保证金";
      });
    },
    /** 修改按钮操作 */
    handleAudit(row) {
      this.form.id = row.id;
      this.title = "审核";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.refundAuditStatus === '2' && !this.form.refundRemark){
            this.$modal.msgError("请填写审核不通过的备注");
            return;
          }
          this.submitLoading = true
          audit(this.form).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.submitLoading = false;
            this.getList();
          }).catch(() => {
            this.submitLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除保证金编号为"' + ids + '"的数据项？').then(function() {
        return delMoney(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/money/export', {
        ...this.queryParams
      }, `money_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
